﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using MusicWorks.Web.Logic.Views.Models;
using WebFormsMvp;
using MusicWorks.Web.Logic.Views;
using MusicWorks.Web.Logic.CatalogService;
using MusicWorks.Web.Logic.Helpers;

namespace MusicWorks.Web.Logic.Presenters
{
    public class CategoryListPresenter : Presenter<ICategoryListView>
    {
        ICatalogService _catalogService;

        public CategoryListPresenter(ICategoryListView view)
            : this(view, ServiceBroker.CreateCatalogServiceInstance())
        {
        }

        public CategoryListPresenter(ICategoryListView view, ICatalogService catalogService)
            : base(view)
        {
            _catalogService = catalogService;
            View.Load += View_Load;
        }

        void View_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(View.ParentCategoryId)) throw new FileNotFoundException();
            var categories = _catalogService.GetChildCategories(View.CatalogName, View.ParentCategoryId);
            View.Model.Categories = categories.Select(x => x.Id).ToList();
           
        }

        public override void ReleaseView()
        {
            View.Load -= View_Load;
        }
    }
}
